using System;
using System.Windows.Forms;
using OutlookBarWorkspaceQuickstart.Common;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using OutlookBarWorkspaceQuickstart.MailModule.Constants;
using Microsoft.Practices.CompositeUI.EventBroker;
using OutlookBarWorkspaceQuickstart.Common.Services;
using System.Drawing;
using SCSFContrib.CompositeUI.WinForms.Workspaces;

namespace OutlookBarWorkspaceQuickstart.MailModule
{
    public class ModuleController : WorkItemController
    {
        private ToolStripService _uiService;

        public override void Run()
        {
            AddServices();
            AddViews();
            Show();
        }        

        private void AddServices()
        {
            _uiService = WorkItem.Services.AddNew<ToolStripService>();
        }
        
        private void AddViews()
        {
            // show the mail folders on the outlook bar. MailFolders implements ISmartPartInfoProvider
            MailFolders folders = WorkItem.Items.AddNew<MailFolders>("MailFolders");
            WorkItem.Workspaces[Constants.WorkspaceNames.OutlookBar].Show(folders);
            
            // add views to the container but don't show them
            WorkItem.Items.AddNew<Inbox>("Inbox");
            WorkItem.Items.AddNew<RightSpine>("RightSpine");            
        }

        [EventSubscription(EventTopicNames.ShowInbox)]
        public void ShowInbox(object sender, EventArgs e)
        {
            Show();
        }

        private void Show()
        {
            _uiService.RegisterToolStrip(new Toolbar().ModuleToolStrip, UIExtensionSiteNames.MainToolbar);
            
            WorkItem.Workspaces[Constants.WorkspaceNames.PrimaryWorkspace].Show(WorkItem.Items.Get("Inbox"));            
            WorkItem.Workspaces[Constants.WorkspaceNames.RightWorkspace].Show(WorkItem.Items.Get("RightSpine"));
        }

        [CommandHandler("New")]
        public void New(object sender, EventArgs e)
        {
            MessageBox.Show("Pressed New default button...");
        }

        [CommandHandler("NewMail")]
        public void NewMail(object sender, EventArgs e)
        {
            MessageBox.Show("Pressed New Mail...");
        }
    }
}
